const express=require('express')
const bodyParser =require('body-parser')
const app=express()
const mysql = require('mysql')//1.引入mysql
app.use(bodyParser.json())

//处理post请求
app.post('/upload',(req,res)=>{
  console.log(req.body)	// 打印客户端请求的传参data数据
  //2.
  var conn = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'a123456',
    database:'student'
  });
  var sql=req.body.sql
 conn.connect()//建立连接
 conn.query(sql,function (err, result) {
   var check=false;//初始状态为false
  if(err){
    console.log('插入信息失败',err.message);
    return;
  }
  else{
   console.log('受影响的行数为：'+result.affectedRows);
   var affect=result.affectedRows
   if(affect==1){
     check=true
     return res.send(check)
   }
   else{
     return res.send(check)
   }
  }
})
})


//处理get请求
app.get('/login',(req,res)=>{
  var connection = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'a123456',
    database:'student'
  });
  //console.log(req.query)//打印客户端传来的数据
  connection.connect()//连接数据库
  var sql="select * from account"//创建sql语句
          connection.query(sql,function (err, result) {
                   if(err){
                     console.log('[SELECT ERROR] - ',err.message);
                     return;
                   }else{
                       //坑1 查询的数据不是json 是一个rowdatapocket对象
                      var dataString = JSON.stringify(result);
                      var data = JSON.parse(dataString);
                      //console.log(data) //打印从数据库中获取到的数据
                      var count=Object.keys(data).length;//获取数据条数
                     // console.log("共有"+count+"个账号")//打印数据条数
                    
                     var check=false
                     var flag=false
                     var m=req.query.username
                     //console.log(m);
                     //进行遍历判断账号是否存在
                    for(i=0;i<=count-1;i++){
                      if(m==data[i].username){
                       flag=true
                      }
                    }

                    if(flag==true){
                      //进行遍历校验信息
                     for(var i=0;i<=count;i++){
                       if(req.query.username==data[i].username){
                         if(req.query.password==data[i].password){
                            check=true
                            return res.send(check)
                         }
                         else{
                          check=false
                           return res.send(check)
                        }
                       }
                     }
                    }
                    else{
                      return res.send(check)
                    }
           }
          }
        )
        })
//查询信息并显示
app.get('/show',(req,res)=>{
  console.log(req.query)
  var connect = mysql.createConnection({
    host:'localhost',
    user:'root',
    password:'a123456',
    database:'student'
  });
  connect.connect()//连接数据库
  var sql=req.query
  connect.query(sql,function(err,result){
    if(err){
      console.log('[SELECT ERROR] - ',err.message);
      return;
    }
    else{
      var dataString = JSON.stringify(result);
      var data = JSON.parse(dataString);
      console.log(data) //打印从数据库中获取到的数据
      return res.send(data)
    }
  })
})
        //启动服务器
app.listen(3000,()=>{
  console.log('server running at http://127.0.0.1:3000')
})

